package ru.inse.core.domain.realty;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Рубрика для недвижимости
 * @author evga
 *
 */
public class RealtyRubric 
{
    static final private Map<Long, RealtyRubric> map = new LinkedHashMap<Long, RealtyRubric>();
    static private RealtyRubric[] array;

    /**Продам*/
    static final public RealtyRubric SELL = new RealtyRubric(1L, "realty.rubric.sell");  
    /**Куплю*/
    static final public RealtyRubric BUY= new RealtyRubric(2L, "realty.rubric.buy");       
    /**Сдам в аренду*/
    static final public RealtyRubric LEASE= new RealtyRubric(4L, "realty.rubric.lease");
    /**Возьму в аренду*/
    static final public RealtyRubric RENT= new RealtyRubric(8L, "realty.rubric.rent");
    /**Обменяю*/
    static final public RealtyRubric CHANGE= new RealtyRubric(16L, "realty.rubric.change");
    /**Сдам в аренду gпосуточно*/
    static final public RealtyRubric LEASE_DAILY= new RealtyRubric(32L, "realty.rubric.daily_hour");

    

    final private Long code;//маска
    final private String labelId;

    private RealtyRubric(Long code, String label)
    {
        this.code = code;
        this.labelId = label;
        map.put(code, this);
        array = null;
    }

    public Long getCode()
    {
        return code;
    }

    public String getLabelId()
    {
        return labelId;
    }
    
    public static RealtyRubric getDefault()
    {
        return SELL;
    }
    
    static public List<RealtyRubric> getByMask(long mask)
    {
        LinkedList<RealtyRubric> result = new LinkedList<RealtyRubric>();
        for(RealtyRubric r : map.values())
        {
            if ((r.getCode() & mask) != 0)
            {
                result.add(r);
            }
        }
        return result;
    }
    
    static public RealtyRubric getByCode(Long code)
    {
        return map.get(code);
    }

    @Override
    public String toString()
    {
        return labelId;
    }
    
    static public RealtyRubric[] list()
    {
        if (array == null)
        {
            synchronized (RealtyRubric.class)
            {
                if (array == null)
                {
                    array = new RealtyRubric[map.size()];
                    int index = 0;
                    for(RealtyRubric r : map.values())
                    {
                        array[index++] = r;
                    }
                }
            }
            
        }
        return array;
    }

}
